package com.qianchang.microfinance.setting;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.OneLineLinearLayout;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述: 密码管理
 * 修 改 人: zhoulai
 * 修改时间: 2017/6/30 16:14
 */
public class PasswordManagerActivity extends BaseActivity {

    private static final String TAG = "PasswordManagerActivity";
    public static final int REQUEST_OK = 101;
    @BindView(R.id.oll_modify_login_password)
    OneLineLinearLayout ollModifyLoginPassword;
    @BindView(R.id.oll_setting_trad_password)
    OneLineLinearLayout ollSettingTradPassword;
    private boolean hasTradPwd;
    public static final String HAS_TRAD_PWD = "hasTradPwd";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_manger);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitleName(getString(R.string.label_password_management));
    }

    @Override
    protected void initData() {
        super.initData();
        getUserInfo();
    }

    private void getUserInfo() {
        show();
        HttpParams params = new HttpParams();
        DMOKHttpUtils.getInstance().post(getApplicationContext(), DMConstant.ApiURL.FIND_TRAD_PWD, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(JsonResult<String> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    String info = response.getObject();//data	0表示已有支付密码、1 表示没有支付密码
                    if ("0".equals(info)) {
                        hasTradPwd = true;
                        ollSettingTradPassword.setLeftContent(getString(R.string.title_modify_transaction_password));//修改支付密码
                    } else {
                        hasTradPwd = false;
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(PasswordManagerActivity.this, msg);// getString(R.string.msg_indentity_code_send_fail));
            }

            @Override
            public void onConnectFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(PasswordManagerActivity.this, msg);
            }
        });

    }


    @OnClick({R.id.oll_modify_login_password, R.id.oll_setting_trad_password})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.oll_modify_login_password:
                startActivity(ModifyPasswordActivity.class);//登录密码修改
                break;
            case R.id.oll_setting_trad_password://支付密码  判断 (没有：设置  ；  有：修改  )
                Intent intent = new Intent(PasswordManagerActivity.this, SettingTradPasswordActivity.class);
                intent.putExtra(HAS_TRAD_PWD, hasTradPwd);
                startActivityForResult(intent, REQUEST_OK);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_OK) {
            if (data != null && data.getBooleanExtra(HAS_TRAD_PWD, false)) {
                hasTradPwd = true;
                ollSettingTradPassword.setLeftContent(getString(R.string.title_modify_transaction_password));//修改支付密码
            }
        }
    }
}
